<?php

class Pikabu_ru extends Grabber_Abstract
{
	protected $_count_grab_news = 1;
	protected $_translate_text = false;
	protected $_convertToUtf8 = 'windows-1251';

	protected function _parsingList( $html )
	{
		$list = array();
		
		$titles = $this->_match('<a.*class="story_link".*>.*<h2.*>(.*)</h2>.*</a>', $html);
		$links  = $this->_match('<a.*style="padding:0".*href="(.*)".*>.*</a>', $html);

		if( !empty( $titles[1] ) && !empty( $links[1] ) )
		{
			foreach( $titles[1] as $key => $value )
			{
				$list[$links[1][$key]] = $value;
			}
		}

		return $list;
	}
	
	protected function _nextListPage( $html )
	{
		$matched = $this->_match('<a.*href="(.*)".*>вперед</a>', $html);//<a.*href="(.*)".*>.*</a>

		if( !empty( $matched[1] ) && !empty( $matched[1][0] ) )
		{
			return 'http://pikabu.ru/index.php' . $matched[1][0];
		}
		
		return false;
	}

	/**
	 * Парсит html, витягивает полную статью
	 *
	 * @param type $html - string
	 * @return array('news'=>string, 'comments'=>array());
	 */
	protected function _getNews( $html )
	{
		$result = array( 'news' => '', 'title' => '', 'comments' => array() );
		
		$html = $this->_cutSpaces( $html );
echo $html; exit;
		$pattern = '<h1>(.*)</h1>';
		$matched = $this->_match( $pattern, $html );
		
		if( !empty( $matched[1] ) && !empty( $matched[1][0] ) )
			$result['title'] = $matched[1][0];

		$pattern = '<div class="entry-content">(.*)</div>';
		$matched = $this->_match( $pattern, $html );

		if( !empty( $matched[1] ) && !empty( $matched[1][0] ) )
			$result['news'] = $matched[1][0];
		
		$pattern = 'class="dsq-comment-message".*><p>(.*)</p>.*</div>';
		$matched = $this->_match( $pattern, $html );
		
		if( !empty( $matched[1] ) )
			$result['comments'] = $matched[1];

		return $result;
	}
	
}
